using System.Numerics;
using Godot;
using System;
using Vector2 = Godot.Vector2;
public class BlockTileSet
{
    public static TileSet tileSet = new TileSet();
    public static int Count;

    public static void RegisterTile(BlockMaterial bm)
    {
        Texture texture;
        if (bm.StateMax>0)
        {
            if (bm.Group != "")
            {
                for (int i = 0; i < bm.StateMax; i++)
                {
                    texture = (Texture)GD.Load($"res://Texture/Block/{bm.Group}/{bm.Name}_{i}.png");
                    int id = CreateTile(bm.Name + "_" + i, texture, bm.Collide, new Vector2(0, 0), bm.shape);
                    if (i == 0) bm.id = id;
                }
            }
            else
            {

                for (int i = 0; i < bm.StateMax; i++)
                {
                    texture = (Texture)GD.Load($"res://Texture/Block/{bm.Name}.png");
                    int id = CreateTile(bm.Name + "_" + i, texture, bm.Collide, new Vector2(i, 0), bm.shape);
                    if (i == 0) bm.id = id;
                }
            }
        }
        else
        {
            if (bm.Group != "")
            {
                texture = (Texture)GD.Load($"res://Texture/Block/{bm.Group}/{bm.Name}.png");
                bm.id = CreateTile(bm.Name, texture, bm.Collide, new Vector2(0, 0), bm.shape);
            }
            else
            {
                texture = (Texture)GD.Load($"res://Texture/Block/{bm.Name}.png");
                bm.id = CreateTile(bm.Name, texture, bm.Collide, new Vector2(0, 0), bm.shape);
            }

        }
    }
    private static int CreateTile(String name, Texture texture, bool collide, Vector2 coord, BlockShape.BlockShapeEnum blockshape)
    {
        tileSet.CreateTile(Count);
        tileSet.TileSetTileMode(Count, TileSet.TileMode.SingleTile);
        tileSet.TileSetTexture(Count, texture);
        tileSet.TileSetRegion(Count, new Rect2(16 * coord.x, 16 * coord.y, 16, 16));

        tileSet.TileSetName(Count, name);
        if (collide)
        {
            RectangleShape2D shape = new RectangleShape2D();

            switch (blockshape)
            {
                case BlockShape.BlockShapeEnum.Block:
                    shape.Extents = new Vector2(8, 8);
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(8, 8)));
                    break;
                case BlockShape.BlockShapeEnum.Half_top:
                    shape.Extents = new Vector2(8, 4);
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(8, 4)));
                    break;
                case BlockShape.BlockShapeEnum.Half_bottom:
                    shape.Extents = new Vector2(8, 4);
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(8, 12)));
                    break;
                case BlockShape.BlockShapeEnum.Chest:
                    shape.Extents = new Vector2(7, 7);
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(8, 9)));
                    break;
                case BlockShape.BlockShapeEnum.Torch:
                    shape.Extents = new Vector2(2, 8);
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(8, 8)));
                    break;
                case BlockShape.BlockShapeEnum.Stairs_left:
                    shape.Extents = new Vector2(4, 4);
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(4, 12)));
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(12, 12)));
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(12, 4)));
                    break;
                case BlockShape.BlockShapeEnum.Stairs_right:
                    shape.Extents = new Vector2(4, 4);
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(4, 12)));
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(12, 12)));
                    tileSet.TileAddShape(Count, shape, new Transform2D(0, new Vector2(4, 4)));
                    break;
            }


        }
        return Count++;
    }
}

public class BlockShape
{
    public enum BlockShapeEnum
    {
        Block, Half_top, Half_bottom, Chest, Torch, Stairs_left, Stairs_right
    }
}